#include "stdafx.h"
#include "string.h"

#if DEBUG

extern "C" HINSTANCE trace_LoadLibraryW( LPCWSTR lpLibFileName )
{
	OutputDebugString(_T("LoadLibraryW: "));
	OutputDebugString(lpLibFileName);
	OutputDebugString(_T("\n"));

	return LoadLibraryW( lpLibFileName );
}

extern "C" LONG trace_RegCloseKey( HKEY hKey )
{
	OutputDebugString(_T("RegCloseKey\n"));
	return RegCloseKey( hKey );
}

extern "C" LONG trace_RegOpenKeyExW( 
	HKEY hKey, 
	LPCWSTR lpSubKey, 
	DWORD ulOptions, 
	REGSAM samDesired, 
	PHKEY phkResult )
{
	OutputDebugString(_T("RegOpenKeyExW: "));

	LONG ret = RegOpenKeyExW( 
		hKey, 
		lpSubKey, 
		ulOptions, 
		samDesired, 
		phkResult );

	// NOTE: return 0 instead of 87
//	if (res == 87) res=0;

#ifdef DEBUG
	TCHAR buf[200];
	swprintf( buf, _T(" hKey=0x%x, %s, opt=0x%x, sam=0x%x resptr=0x%x ret=%d\n"), hKey, lpSubKey, ulOptions, samDesired, phkResult, ret );
	OutputDebugString(buf);
#endif

	return ret;
}

extern "C" LONG trace_RegQueryValueExW(
  HKEY hKey,
  LPCTSTR lpValueName,
  LPDWORD lpReserved,
  LPDWORD lpType,
  LPBYTE lpData,
  LPDWORD lpcbData
)
{
	OutputDebugString(_T("RegQueryValueExW: "));
	OutputDebugString(lpValueName);

	LONG res = RegQueryValueEx(
		hKey,
		lpValueName,
		lpReserved,
		lpType,
		lpData,
		lpcbData );

	// NOTE: return 0 instead of 87
//	if (res == 87) res=0;

#ifdef DEBUG
	TCHAR buf[30];
	swprintf( buf, _T(" res=%d\n"), res );
	OutputDebugString(buf);

	OutputDebugString( (LPCTSTR) lpData );
	OutputDebugString( _T("\n") );
#endif

	return res;
}

extern "C" BOOL trace_SystemParametersInfoW(
  UINT uiAction,
  UINT uiParam,
  PVOID pvParam,
  UINT fWinIni
)
{
	OutputDebugString(_T("SystemParametersInfoW\n"));

	return SystemParametersInfoW(
		uiAction,
		uiParam,
		pvParam,
		fWinIni );
}


extern "C" FARPROC trace_GetProcAddressW( HMODULE hModule, LPCWSTR lpProcName)
{
	OutputDebugString(_T("GetProcAddressW: "));
	OutputDebugString(lpProcName);
	OutputDebugString(_T("\n"));

	return GetProcAddressW( hModule, lpProcName);
}


extern "C" VOID trace_GetSystemInfo( LPSYSTEM_INFO lpSystemInfo)
{
	OutputDebugString(_T("GetSystemInfo\n"));
	GetSystemInfo( lpSystemInfo);
}

extern "C" HANDLE trace_CreateFileW(
	LPCTSTR lpFileName, 
	DWORD dwDesiredAccess, 
	DWORD dwShareMode, 
	LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
	DWORD dwCreationDispostion , 
	DWORD dwFlagsAndAttributes, 
	HANDLE hTemplateFile)
{
	OutputDebugString(_T("CreateFileW: "));
	OutputDebugString(lpFileName);
	OutputDebugString(_T("\n"));

	return CreateFile(
		lpFileName, 
		dwDesiredAccess, 
		dwShareMode, 
		lpSecurityAttributes, 
		dwCreationDispostion , 
		dwFlagsAndAttributes, 
		hTemplateFile);
}

extern "C" HANDLE trace_CreateFileForMappingW( 
	LPCWSTR lpFileName, 
	DWORD dwDesiredAccess, 
	DWORD dwShareMode, 
	LPSECURITY_ATTRIBUTES lpSecurityAttributes, 
	DWORD dwCreationDisposition, 
	DWORD dwFlagsAndAttributes, 
	HANDLE hTemplateFile 
	)
{
	OutputDebugString(_T("CreateFileForMappingW: ")); 
	OutputDebugString(lpFileName);

	HANDLE hFile = CreateFileForMapping( 
		lpFileName, 
		dwDesiredAccess, 
		dwShareMode, 
		lpSecurityAttributes, 
		dwCreationDisposition, 
		dwFlagsAndAttributes, 
		hTemplateFile 
	);

#ifdef DEBUG
	TCHAR buf[30];
	swprintf( buf, _T(" hFile=0x%x\n"), hFile );
	OutputDebugString(buf);
#endif

	return hFile;
}

extern "C" DWORD trace_GetModuleFileNameW( 
	HMODULE hModule,
	LPWSTR lpFilename, 
	DWORD nSize)
{
	OutputDebugString(_T("GetModuleFileNameW: ")); 

	DWORD res=GetModuleFileNameW( hModule, lpFilename, nSize );

	OutputDebugString(lpFilename); 
	OutputDebugString(_T("\n")); 

	return res;
}

extern "C" size_t trace_wcslen( const wchar_t *string )
{
	OutputDebugString(_T("wcslen: "));
	OutputDebugString(string);
	OutputDebugString(_T("\n"));

	return wcslen( string );
}

extern "C" HANDLE trace_CreateFileMappingW(
	HANDLE hFile, 
	LPSECURITY_ATTRIBUTES lpFileMappingAttributes, 
	DWORD flProtect, 
	DWORD dwMaximumSizeHigh, 
	DWORD dwMaximumSizeLow, 
	LPCWSTR lpName )
{
//	OutputDebugString(_T("CreateFileMappingW: "));
//	OutputDebugString(lpName);
//	OutputDebugString(_T("\n"));

	HANDLE ret = CreateFileMapping(
		hFile, 
		lpFileMappingAttributes, 
		flProtect, 
		dwMaximumSizeHigh, 
		dwMaximumSizeLow, 
		lpName );

	TCHAR buf[100];
	swprintf( buf, _T("CreateFileMappingW: 0x%x, %s ret: 0x%x\n"), hFile, lpName, ret );
	OutputDebugString(buf);

	return ret;
}

extern "C" DWORD trace_GetLastError()
{
	DWORD dw = GetLastError();

//	DebugBreak();

	TCHAR buf[100];
	swprintf( buf, _T("GetLastError: 0x%x \n"), dw );
	OutputDebugString(buf);

//if (0x57 == dw) dw = 0;

	return dw;
}

extern "C" void trace_SetLastError( DWORD dw )
{
	TCHAR buf[100];
	swprintf( buf, _T("SetLastError: 0x%x \n"), dw );
	OutputDebugString(buf);

	SetLastError( dw );
}

#if !NET1SP3
extern "C" BOOL trace_GetVersionExW( LPOSVERSIONINFO lpVersionInfo )
{ 
	OutputDebugString(_T("GetVersionExW: "));

	OSVERSIONINFO versionInfo;
	versionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

	TCHAR buf[100];
	swprintf( buf, _T(" lpVersionInfo=0x%x"), lpVersionInfo );
	OutputDebugString(buf);

	swprintf( buf, _T(" size=%d, standard size=%d\n"), lpVersionInfo->dwOSVersionInfoSize, versionInfo.dwOSVersionInfoSize);
	OutputDebugString(buf);

	if ( GetVersionEx( lpVersionInfo ) )
	{
		swprintf( buf, _T(" dwMajorVersion=0x%x res = OK\n"), lpVersionInfo->dwMajorVersion );
		OutputDebugString(buf);
		return true;
	}
	else
	{
		OutputDebugString(_T(" res = ERROR\n"));
		return false;
	}
}
#endif // !NET1SP3

/*
void FormatMessageW()
{
	OutputDebugString(_T("FormatMessageW\n"));
	DebugBreak();
}

void RegSetValueExW()
{
	DebugBreak();
}
*/

extern "C" char *trace_strncpy( char *strDest, const char *strSource, size_t count )
{
	TCHAR buf[100];
	swprintf( buf, _T("strncpy: %S\n"), strSource );
	OutputDebugString(buf);

	return strncpy( strDest, strSource, count );
}

extern "C" BOOL trace_CloseHandle(   HANDLE hObject )
{
	TCHAR buf[100];
	swprintf( buf, _T("CloseHandle: 0x%x\n"), hObject );
	OutputDebugString(buf);

	return CloseHandle( hObject );
}

extern "C" int trace_MultiByteToWideChar(
  UINT CodePage, 
  DWORD dwFlags, 
  LPCSTR lpMultiByteStr, 
  int cbMultiByte, 
  LPWSTR lpWideCharStr, 
  int cchWideChar 
)
{
	int ret = MultiByteToWideChar( 
		CodePage, 
		dwFlags, 
		lpMultiByteStr, 
		cbMultiByte, 
		lpWideCharStr, 
		cchWideChar );

	TCHAR buf[100];
	swprintf( buf, _T("MultiByteToWideChar: %s\n"), lpWideCharStr );
	OutputDebugString(buf);

	return ret;
}

extern "C" int trace_WideCharToMultiByte(
  UINT CodePage, 
  DWORD dwFlags, 
  LPCWSTR lpWideCharStr, 
  int cchWideChar, 
  LPSTR lpMultiByteStr, 
  int cbMultiByte, 
  LPCSTR lpDefaultChar, 
  LPBOOL lpUsedDefaultChar 
)
{
	int ret = WideCharToMultiByte(
		CodePage, 
		dwFlags, 
		lpWideCharStr, 
		cchWideChar, 
		lpMultiByteStr, 
		cbMultiByte, 
		lpDefaultChar, 
		lpUsedDefaultChar );

	TCHAR buf[100];
	swprintf( buf, _T("WideCharToMultiByte: %s\n"), lpWideCharStr );
	OutputDebugString(buf);

	return ret;
}

extern "C" HANDLE trace_FindFirstFileW(
  LPCTSTR lpFileName, 
  LPWIN32_FIND_DATA lpFindFileData 
)
{
	TCHAR buf[100];
	swprintf( buf, _T("FindFirstFile: %s\n"), lpFileName );
	OutputDebugString(buf);


	return FindFirstFile(
	  lpFileName, 
	  lpFindFileData );
}

extern "C" char* trace_strchr( const char *string, int c )
{
	TCHAR buf[1000];
	swprintf( buf, _T("strchr: %S\n"), string );
	OutputDebugString(buf);

	return strchr( string, c );	
}

#endif // DEBUG
